<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加博客或者修改</title>
</head>

<body>
    <table>
        <tr>
            <td><label for="">标题:</label></td>
            <td><input type="text" placeholder="请输入标题" name="title"></td>
        </tr>
        <tr>
            <td><label for="">摘要:</label></td>
            <td><input type="text" placeholder="请输入摘要" name="abstract"></td>
        </tr>
        <tr>
            <td><label for="">内容:</label></td>
            <td><input type="text" placeholder="请输入内容" name="content"></td>
        </tr>
        <tr>
            <td><label for="">分类:</label></td>
            <td><input type="text" placeholder="请输入分类" name="classify"></td>
        </tr>
        <tr>
            <td><label for="">作者:</label></td>
            <td><input type="text" placeholder="请输入作者" name="author"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script src="./js/init.js"></script>
    <script>

        $(function (){
            let id=getUrlParam('id')
            id=id || 0;
            getBlogById(id).then(res=>{
                let obj=res.data;
                $('[name=title]').val(obj.title);
                $('[name=abstract]').val(obj.abstract);
                $('[name=content]').val(obj.content);
                $('[name=classify]').val(obj.classify);
                $('[name=author]').val(obj.author);
            })
        })
        //拿到url参数
        function getUrlParam(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

            var r = window.location.search.substr(1).match(reg); //匹配目标参数

            if (r != null) {
                return unescape(r[2]);
            } else {
                return null; //返回参数值
            }

        }
        //判断是否拿到id，拿到进行修改操作，没拿到进行添加操作
        function save(){
            let id=getUrlParam('id')
            let obj={
                title:$('[name=title]').val(),
                abstract:$('[name=abstract]').val(),
                content:$('[name=content]').val(),
                classify:$('[name=classify]').val(),
                author:$('[name=author]').val(),
            }
            if(id){
                updateBlog(id,obj).then(res=>{
                    location.href='./index.html'
                })
            }else{
                let params={
                    title:obj.title,
                    abstract:obj.abstract,
                    content:obj.content,
                    classify:obj.classify,
                    author:obj.author,
                }
                addBlog(params).then(res=>{
                    location.href='./index.html'
                })
            }
        }
        //点击后跳转首页
        function cancel(){
            location.href='./index.html'
        }

    </script>
</body>

</html>